package com.shengwei.truck.driver.view.ui.common;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TimePicker;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.view.ui.statistic.HistroyStatisticActivity;

import java.util.Calendar;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DatePickerFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {
    private int mDlgType;

    public DatePickerFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment DatePickerFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static DatePickerFragment newInstance(int dateTimeDlgType) {
        DatePickerFragment fragment = new DatePickerFragment();
        Bundle args = new Bundle();
        args.putInt("dlgType", dateTimeDlgType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mDlgType = getArguments().getInt("dlgType");
        }
    }
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog =  getDateTimeDlg(mDlgType);
        return dialog;
    }

    /**
     * 得到 "日期" 或 "时间" 选择对话框
     * @param dlgType
     * @return
     */
    public Dialog getDateTimeDlg(int dlgType){
        if(dlgType == 1){
            final Calendar c = Calendar.getInstance();   //调用getInstance
            int year = c.get(Calendar.YEAR);    // 得到年
            int month = c.get(Calendar.MONTH);   //月
            int day = c.get(Calendar.DAY_OF_MONTH);   //日
           return  new DatePickerDialog(getActivity(),this,year,month,day);
        }
        else{
            return null;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_date_picker, container, false);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        ((HistroyStatisticActivity)getActivity()).processDatePickerResult(year,month,dayOfMonth);
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

    }
}